package com.msun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.msun.constant.MessageConstant;
import com.msun.constant.RedisConstant;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.entity.Result;
import com.msun.pojo.Plan;
import com.msun.pojo.Setmeal;
import com.msun.service.SetmealService;
import com.msun.utils.QcloudUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.FileInputStream;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 体检套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //使用JedisPool操作Redis服务
    @Autowired
    private JedisPool jedisPool;

   /* //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();//原始文件名 3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);//.jpg
        String fileName = UUID.randomUUID().toString() + extention;//	FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }*/
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        // 上传过来的实际文件名
        String filenameUP = imgFile.getOriginalFilename();
        int i = filenameUP.lastIndexOf(".");
        String suffix = filenameUP.substring(i);
        // 真实使用的文件名
        String fileName = UUID.randomUUID().toString()+suffix;
        long size = imgFile.getSize();
        try {
            QcloudUtils.putObject(fileName, (FileInputStream) imgFile.getInputStream(),size);
//            添加图片名到redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }
    @Reference
    private SetmealService setmealService;

    //新增套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("hello");
        try {
            //  如果用了分组,再第二页会搜不到第一页中的数据
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
    //查询套餐
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Setmeal> list = setmealService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PostMapping("/save")
    public Result saveResult(@RequestBody Map<String, Object> map){
//        setmealService.save(map);
return new Result(true,"保存成功",null);
    }
    @RequestMapping("/deleteById/{id}")
    public Result deleteById( @PathVariable Integer id){
        System.out.println(id);
        try {
//            Integer id  = Integer.parseInt(ids);
            setmealService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL1);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        System.out.println(map);
        try {
            setmealService.edit(map);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
